package com.baijiayun.liveuibase.zxing;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.view.KeyEvent;

import com.baijia.bjydialog.MaterialDialog;
import com.baijiayun.liveuibase.base.BaseScreenActivity;
import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.DisplayUtils;
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

/**
 * Created by pengwenjian on 2020/11/11
 * Describe: 二维码扫描页面
 */
public class ScanActivity extends BaseScreenActivity implements DecoratedBarcodeView.TorchListener {
    private static final int REQUEST_CODE_PERMISSION_CAMERA = 0x01;
    private DecoratedBarcodeView dBarcodeView;
    private CaptureManager captureManager;
    private Bundle savedInstanceState;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;
        init();
        startScan();
    }

    @Override
    public int getLayoutId() {
        return R.layout.uibase_activity_scanner;
    }

    protected void init() {
        dBarcodeView = findViewById(R.id.dbarcode_view);
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
        findViewById(R.id.tv_scanner).setBackground(new DrawableBuilder()
                .rectangle()
                .cornerRadius(DisplayUtils.dip2px(this,30))
                .solidColor(0x4dffffff)
                .build());
    }


    public void startScan() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
            dBarcodeView.setTorchListener(this);
            captureManager = new CaptureManager(this, dBarcodeView);
            captureManager.initializeFromIntent(getIntent(), savedInstanceState);
            captureManager.decode();
        } else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CODE_PERMISSION_CAMERA);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_CODE_PERMISSION_CAMERA) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startScan();
            } else {
                showSystemSettingDialog();
            }
        }
    }

    private void showSystemSettingDialog() {
        new MaterialDialog.Builder(this)
                .title(getString(R.string.base_live_sweet_hint))
                .content(getString(R.string.base_live_no_camera_permission))
                .positiveText(getString(R.string.base_live_confirm))
                .positiveColor(ContextCompat.getColor(this,R.color.base_toolbar_blue))
                .onPositive((dialog, which) -> {
                    dialog.dismiss();
                })
                .canceledOnTouchOutside(true)
                .build()
                .show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (captureManager != null) {
            captureManager.onPause();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (captureManager != null) {
            captureManager.onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (captureManager != null) {
            captureManager.onDestroy();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        if (captureManager != null) {
            captureManager.onSaveInstanceState(outState);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return dBarcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    @Override
    public void onTorchOn() {
        // do nothing
    }

    @Override
    public void onTorchOff() {
        // do nothing
    }
}